package com.dfbz.demo05_Field;

import org.junit.Test;

import java.lang.reflect.Field;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo01_反射类的成员变量 {

    @Test
    public void test1() throws Exception {

        // 1. 字节码对象
        Class<TestEntity> testEntityClass = TestEntity.class;

        // 2. 反射字段(public修饰的成员不需要使用getDeclaredField)
        Field usernameField= testEntityClass.getField("username");

        // 获取任意权限修饰符 修饰的成员
        Field passwordField= testEntityClass.getDeclaredField("password");
        Field phoneField= testEntityClass.getDeclaredField("phone");
        Field emailField= testEntityClass.getDeclaredField("email");

        System.out.println(usernameField);
        System.out.println(passwordField);
        System.out.println(phoneField);
        System.out.println(emailField);
    }


    @Test
    public void test2() throws Exception {

        // 1. 字节码对象
        Class<TestEntity> testEntityClass = TestEntity.class;

        // 获取所有public修饰的字段
        Field[] fields = testEntityClass.getFields();

        for (Field field : fields) {
            System.out.println(field);
        }

        System.out.println("----------------");

        // 获取任意权限修饰符修饰的成员变量(public修饰的也行)
        Field[] declaredFields = testEntityClass.getDeclaredFields();

        for (Field field : declaredFields) {
            System.out.println(field);
        }

        System.out.println("-----------------");
    }
}
